//
//  UIButton+AWRepeatClick.m
//  AWBase
//
//  Created by Alan Wang on 2018/6/29.
//  Copyright © 2018年 Alan Wang. All rights reserved.
//

#import "UIButton+AWRepeatClick.h"
#import <objc/runtime.h>
#import "JFBasePri.h"
@interface UIButton()

@property (nonatomic, assign) NSTimeInterval awAcceptEventTime;

@property(nonatomic,assign)BOOL isIgnoreEvent;

@end

@implementation UIButton (AWRepeatClick)
+ (void)load{
//    Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
//    SEL sysSEL = @selector(sendAction:to:forEvent:);
//
//    Method customMethod = class_getInstanceMethod(self, @selector(custom_sendAction:to:forEvent:));
//    SEL customSEL = @selector(custom_sendAction:to:forEvent:);
//    BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(customMethod), method_getTypeEncoding(customMethod));
//    if (didAddMethod) {
//        class_replaceMethod(self, customSEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
//    }else{
//        method_exchangeImplementations(systemMethod, customMethod);
//
//    }
}

- (NSTimeInterval )awAcceptEventInterval{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
}

- (void)setAwAcceptEventInterval:(NSTimeInterval)awAcceptEventInterval{
    objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(awAcceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent
{
    objc_setAssociatedObject(self,@"UIControl_AW_isIgnoreEvent", @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isIgnoreEvent
{
    return [objc_getAssociatedObject(self, @"UIControl_AW_isIgnoreEvent") boolValue];
}
- (void)resetState
{
    [self setIsIgnoreEvent:NO];
}

- (void)setOpen:(BOOL)open
{
    objc_setAssociatedObject(self, "UIControl_AW_open", @(open), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)open
{
    return [objc_getAssociatedObject(self, "UIControl_AW_open") boolValue];
}





- (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    
    NSString * actionStr = NSStringFromSelector(action);
    
    if ([actionStr hasPrefix:@"growing"]) {
        [self custom_sendAction:action to:target forEvent:event];
        return;
    }
    
    if (!self.open) {//只有开启了的才走避免重复点击的逻辑
        if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"] ) {
            self.awAcceptEventInterval = self.awAcceptEventInterval == 0 ? 0.1 : self.awAcceptEventInterval;
            if (self.isIgnoreEvent) {
                return;
            } else if (self.awAcceptEventInterval > 0) {
                WEAKSELF
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, self.awAcceptEventInterval*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    StrongSelf(weakSelf)
                    [strongSelf resetState];
                });
            }
        }
        //此处methodA和methodB方法IMP互换了，实际上执行sendAction；所以不会死循环
        self.isIgnoreEvent = YES;
        [self custom_sendAction:action to:target forEvent:event];
    }else{
        [self custom_sendAction:action to:target forEvent:event];
    }
}
@end
